/***************************************************************************
 * Copyright 2012-2013 TXT e-solutions SpA
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the contract ICT-257367.
 *
 * Authors:
 *      Salvatore Piccione (TXT e-solutions SpA)
 *
 * Contributors:
 *        Domenico Rotondi (TXT e-solutions SpA)
 **************************************************************************/
package it.txt.ens.core.util.parser;

import it.txt.ens.core.ENSAuthzServiceConnectionParameters;
import it.txt.ens.core.factory.ENSAuthzServiceConnectionParametersFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT txtgroup.com)
 * @author Domenico Rotondi (TXT e-solutions SpA - domenico.rotondi AT txtgroup.com)
 *
 */
public class ENSConnectionParametersMapParser {
    
    public static ENSAuthzServiceConnectionParameters parse (ENSAuthzServiceConnectionParametersFactory factory,
            Map<?,?> propertyMap) throws ParseException, IllegalArgumentException {
        if (factory == null)
            throw new IllegalArgumentException("The factory cannot be null");
        if (propertyMap == null)
            throw new IllegalArgumentException("The property container cannot be null");
        if (propertyMap.isEmpty())
            throw new IllegalArgumentException("The property container cannot be empty");
        List<String> missingProperties = new ArrayList<String>(
                ENSAuthzServiceConnectionParametersFactory.NUMBER_OF_MANDATORY_PROPERTIES);
        try {

            String subjectID;
            if ((subjectID = (String) propertyMap.get(ENSAuthzServiceConnectionParametersFactory.SUBJECT_ID)) == null)
                missingProperties.add(ENSAuthzServiceConnectionParametersFactory.SUBJECT_ID);
            
            String accessToken;
            if ((accessToken = (String) propertyMap.get(ENSAuthzServiceConnectionParametersFactory.ACCESS_TOKEN)) == null)
                missingProperties.add(ENSAuthzServiceConnectionParametersFactory.ACCESS_TOKEN);
            
            String destinationName;
            if ((destinationName = (String) propertyMap.get(ENSAuthzServiceConnectionParametersFactory.DESTINATION_NAME)) == null)
                missingProperties.add(ENSAuthzServiceConnectionParametersFactory.DESTINATION_NAME);
                
            //port parse
            Object portObj = propertyMap.get(ENSAuthzServiceConnectionParametersFactory.BROKER_PORT);
            int port;
            if (portObj == null)
                port = ENSAuthzServiceConnectionParametersFactory.DEFAULT_BROKER_PORT;
            else if (portObj instanceof String) {
                try {
                    port = Integer.parseInt((String) portObj);
                } catch (NumberFormatException e) {
                    throw new ParseException("The value of the property " + ENSAuthzServiceConnectionParametersFactory.BROKER_PORT + 
                        " must be a valid integer number. Given value: " + portObj);
                }
            } else if (portObj instanceof Integer)
                port = (Integer) portObj;
            else
                throw new ParseException("The value of the property " + ENSAuthzServiceConnectionParametersFactory.BROKER_PORT + 
                    " must be an instance of either " + Integer.class.getName() +
                    " or " + String.class.getName() + ". Value class: " + 
                    portObj.getClass().getName());
            
            portObj = propertyMap.get(ENSAuthzServiceConnectionParametersFactory.MGMT_PORT);
            int mgmtPort;
            if (portObj == null)
                mgmtPort = ENSAuthzServiceConnectionParametersFactory.DEFAULT_MGMT_PORT;
            else if (portObj instanceof String) {
                try {
                    mgmtPort = Integer.parseInt((String) portObj);
                } catch (NumberFormatException e) {
                    throw new ParseException("The value of the property " + ENSAuthzServiceConnectionParametersFactory.MGMT_PORT + 
                        " must be a valid integer number. Given value: " + portObj);
                }
            } else if (portObj instanceof Integer)
                mgmtPort = (Integer) portObj;
            else
                throw new ParseException("The value of the property " + ENSAuthzServiceConnectionParametersFactory.MGMT_PORT + 
                    " must be an instance of either " + Integer.class.getName() +
                    " or " + String.class.getName() + ". Value class: " + 
                    portObj.getClass().getName());
            
            return factory.create(subjectID, accessToken, (String) propertyMap.get(
                    ENSAuthzServiceConnectionParametersFactory.BROKER_HOST),
                    port,
                    mgmtPort,
                    (String) propertyMap.get(ENSAuthzServiceConnectionParametersFactory.VIRTUAL_HOST),
                    (String) destinationName, 
                    Boolean.parseBoolean((String) propertyMap.get(ENSAuthzServiceConnectionParametersFactory.TLS_ENABLED)));
        } catch (ClassCastException e) {
            throw new ParseException("An error occurred while casting the value of a property",e);
        }
    }
}
